/*
   Copyright [2013] [szhu1@umbc.edu]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package szhu.hcc.umbc.crowdsourcing.quality.core;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Unit test for {@link QualityJudgingCoreImpl}
 *
 * @author szhu1@umbc.edu
 */
@RunWith(JUnit4.class)
public class QualityJudgingCoreTest {

    @Test
    public void testExtractTrainingDataSet() {
        QualityJudgingCoreImpl judgingCoreImpl = new QualityJudgingCoreImpl();
        List<Integer> chosenIds = Arrays.asList(1, 2, 3);
        RawData data_1 = new RawData(0, "1", 1, Arrays.asList(1.0, 3.0, 5.0), Arrays.asList(10.0, 20.0, 70.0));
        RawData data_2 = new RawData(0, "2", 1, Arrays.asList(1.0, 3.0, 5.0), Arrays.asList(5.0, 30.0, 20.0));
        RawData data_3 = new RawData(0, "3", -1, Arrays.asList(1.0, 3.0, 5.0), Arrays.asList(16.0, 25.0, 50.0));
        RawData data_4 = new RawData(0, "3", -1, Arrays.asList(1.0, 3.0, 5.0), Arrays.asList(56.0, 34.0, 15.0));
        int user_per_task = 4;
        List<RawData> rawDataList = Arrays.asList(data_1, data_2, data_3, data_4);
        ArrayList[] allChosenTaskNatures = new ArrayList[data_1.taskNatures.size()];
        ArrayList[] effortUpperLimits = new ArrayList[data_1.userEfforts.size()];
        ArrayList[] effortLowerLimits = new ArrayList[data_1.userEfforts.size()];

        judgingCoreImpl.extractTrainingDataSet(chosenIds, rawDataList, allChosenTaskNatures, effortUpperLimits,
                effortLowerLimits, user_per_task, new ArrayList<String>());
        // minimal of two qualified for the upper
        double upper_0 = Math.min(data_1.userEfforts.get(0), data_2.userEfforts.get(0));
        double lower_0 = Math.min(data_3.userEfforts.get(0), upper_0 - 1);
        assertEquals(upper_0, effortUpperLimits[0].get(0));
        assertEquals(lower_0, effortLowerLimits[0].get(0));
    }

    @Test
    public void testComputeExpectedValue() {
        QualityJudgingCoreImpl judgingCoreImpl = new QualityJudgingCoreImpl();
        List<Double> taskNatures = Arrays.asList(1.0, 2.0, 3.0);
        double[][] params = new double[][]{{4, 3, 2, 1}, {3, 2, 4, 1}};
        double[] expectResults = new double[]{14.0, 16.0};
        checkDiff(expectResults, judgingCoreImpl.computeExpectedValueWithRegressionParams(taskNatures, params));
    }

    @Test
    public void testApacheGetLinearRegressionParams() {
        QualityJudgingCore judgingCore = new QualityJudgingCoreImpl();
        ArrayList[] taskNatures = new ArrayList[3];
        taskNatures[0] = new ArrayList(Arrays.asList(1, 2, 3, 2, 3));
        taskNatures[1] = new ArrayList(Arrays.asList(2, 1, 5, 7, 10));
        taskNatures[2] = new ArrayList(Arrays.asList(4, 5, 7, 3, 7));
        ArrayList[] efforts = new ArrayList[2];
        efforts[0] = new ArrayList(Arrays.asList(1234, 32134, 22345, 1234, 3977));
        efforts[1] = new ArrayList(Arrays.asList(132378, 34324, 324234, 43545, 54365));
        double[][] expects = new double[][]{{-1588.340, 22288.278, -5009.520, -1964.130},
                {-43180.644, 30187.618, -12804.566, 30492.291}};
        double[][] computed = judgingCore.getLinearRegressionParams(taskNatures, efforts);
        checkDiff(expects, computed);
    }

    private void checkDiff(double[][] expects, double[][] computed) {
        double accept_diff = 0.001;
        if (expects.length != computed.length || expects[0].length != computed[0].length) {
            fail("results Size not match");
        }
        for (int i = 0; i < expects.length; i++) {
            for (int j = 0; j < expects[0].length; j++) {
                if (Math.abs(expects[i][j] - computed[i][j]) > accept_diff) {
                    fail("Results do not match" + "Expect: " + expects[i][j] + ";\t Computed: " + computed[i][j]);
                }
            }
        }
    }

    private void checkDiff(double[] expects, double[] computed) {
        double accept_diff = 0.001;
        if (expects.length != computed.length) {
            fail("results Size not match");
        }
        for (int i = 0; i < expects.length; i++) {
            if (Math.abs(expects[i] - computed[i]) > accept_diff) {
                fail("Results do not match" + "Expect: " + expects[i] + ";\t Computed: " + computed[i]);
            }
        }
    }
}
